/* 
 *  Created By: Mike Stevens
 *  Project URL: http://www.codeplex.com/sitesecuritymgmt
 *  License: GNU GPL v2
 *  Copyright (C) 2008 Mike Stevens
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web;

namespace SiteSecurityManagement.Service.SecurityTree
{
    class SecurityTreeState
    {
        #region Members
        #endregion

        #region Constructor/Destructor
        #endregion

        #region Properties
        #endregion

        #region Methods
        /// <summary>
        /// Determines whether the state is saved.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns>
        /// 	<c>true</c> if state is saved otherwise, <c>false</c>.
        /// </returns>
        public static bool IsStateSaved(string key)
        {
            if (HttpContext.Current.Session[key] == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Saves the expanded node.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="key">The key.</param>
        public static void SaveExpandedNode(TreeNode node, string key)
        {
            Dictionary<string,string> dict = new Dictionary<string,string>();
            if (HttpContext.Current.Session[key] != null)
            {
                dict = (Dictionary<string,string>)HttpContext.Current.Session[key];
            }

            if (false == dict.ContainsKey(node.Value))
            {
                dict.Add(node.Value, node.Text);
            }
            HttpContext.Current.Session[key] = dict;
        }

        /// <summary>
        /// Removes the expanded node.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="key">The key.</param>
        public static void RemoveExpandedNode(TreeNode node, string key)
        {
            if (HttpContext.Current.Session[key] != null)
            {
                Dictionary<string, string> dict = (Dictionary<string, string>)HttpContext.Current.Session[key];
                dict.Remove(node.Value);
                if (node.ChildNodes.Count > 0)
                {
                    RemoveChildExpandedNodes(node.ChildNodes, key);
                }
                HttpContext.Current.Session[key] = dict;
            }
        }

        /// <summary>
        /// Removes the child expanded nodes.
        /// </summary>
        /// <param name="nodes">The nodes.</param>
        /// <param name="key">The key.</param>
        private static void RemoveChildExpandedNodes(TreeNodeCollection nodes, string key)
        {
            Dictionary<string, string> dict = (Dictionary<string, string>)HttpContext.Current.Session[key];

            foreach (TreeNode node in nodes)
            {
                if (dict.ContainsKey(node.Value))
                {
                    RemoveExpandedNode(node, key);
                }
            }
        }

        /// <summary>
        /// Restores the tree view.
        /// </summary>
        /// <param name="treeView">The tree view.</param>
        /// <param name="key">The key.</param>
        public static void RestoreTreeView(TreeView treeView, string key)
        {
            RestoreTreeViewExpandedState(treeView.Nodes,
                (Dictionary<string, string>)HttpContext.Current.Session[key] ?? new Dictionary<string, string>());
        }

        /// <summary>
        /// Restores the state of the tree view expanded.
        /// </summary>
        /// <param name="nodes">The nodes.</param>
        /// <param name="dict">The dict.</param>
        private static void RestoreTreeViewExpandedState(TreeNodeCollection nodes, Dictionary<string, string> dict)
        {
            foreach (TreeNode node in nodes)
            {
                if (dict.ContainsKey(node.Value))
                {
                    node.Expanded = true;
                }

                if (node.ChildNodes.Count > 0)
                {
                    RestoreTreeViewExpandedState(node.ChildNodes, dict);
                }
            }
        }
        #endregion
    }
}
